package controllers

import "myerp/models"
import "myerp/src"
import "time"

//出库
type OutgoingController struct {
	CommonController
}

//添加
func (c *OutgoingController) Add() {
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 0
		reData["msg"] = "添加失败！"
		var name string
		var deid int
		var err error
		if name = c.GetString("name"); name == "" {
			reData["msg"] = "入库主题不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		if deid, err = c.GetInt("deid"); err != nil {
			reData["msg"] = "仓库不存在"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		var md models.Outgoing
		md.Uid = int(UserInfo.Id)
		md.Number = src.OrderNumber("OUT")
		md.Ctime = int(time.Now().Unix())
		md.Name = name
		md.Markte = c.GetString("markte")
		md.Deid = deid
		md.Goods = c.GetStrings("goods[]")
		md.GoodsNum = c.GetStrings("GoodsNum[]")
		_, err = models.AddOutgoing(&md)
		if err != nil {
			reData["msg"] = "参数不正确，JS没有加载完成"
		} else {
			c.DelSession("outgoingorder")
			reData["status"] = 1
			reData["url"] = "/outgoing/list"
		}
		c.Data["json"] = reData
		c.ServeJSON()
		return

	}
	//获取商品
	gid := c.GetSession("outgoingorder")
	if gid == nil {
		c.Redirect("/outgoing/list", 302)
		return
	}
	_, gl := models.GetGoodsList(1, 1000, "my_goods.id in("+gid.(string)+")")
	c.Data["glist"] = gl
	//获取仓库列表
	l, _ := models.GetAllDepotNotPage()
	c.Data["depot"] = l
	c.Data["ActionName"] = "添加出库单"
	c.Layout = "base.html"
	c.TplName = "outgoing/add.html"
}

func (c *OutgoingController) details() {
	var id int
	var err error
	id, err = c.GetInt("id")
	if err != nil {
		c.Redirect("/outgoing/list", 302)
		return
	}
	l, err := models.GetOutgoingById(id)
	if err != nil {
		c.Redirect("/outgoing/list", 302)
		return
	}
	c.Data["infos"] = l
	c.Data["ActionName"] = "入库详情"
	c.Layout = "base.html"
	c.TplName = "outgoing/details.html"
}

//列表
func (c *OutgoingController) List() {
	c.Data["ActionName"] = "出库管理"
	page, _ := c.GetInt("page", 1)
	count, l := models.GetOutgoingList(int64(page), 10, "")
	c.Data["List"] = l
	var pages src.Page = src.NewPage(page, 10, int(count), "/outgoing/list")
	c.Data["page"] = pages.Show()
	c.Layout = "base.html"
	c.TplName = "outgoing/list.html"
}
